﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NVR.BLL
{

    public class DeviceTable
    {
        private DALFactory.DALFactory dataFactory = DataProvider.DefaultProvider;
        /// <summary>
        /// 得到设备信息虚表信息。根据传入的用户名来决定该显示那些记录。
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="counts"></param>
        /// <param name="pageNums"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public List<Entity.DeviceTable> GetDeviceTable(int currentPage,int pageSize,out int counts,out int pageNums,string username)
        {
            List<Entity.DeviceTable> dtList = new List<Entity.DeviceTable>();
            UserInfo user = new UserInfo();
            NVRDevInfo nvr = new NVRDevInfo();
            DeviceInfo dev = new DeviceInfo();
            counts = 0;
            pageNums = 0;
            string userType = user.GetUserType(username);
            if (userType == "超级管理员")
            {
                //得到每页记录数。然后再查NVRDeviceInfo表，这样查询的效率
                //比将两个联合起来再比较的效率高
                List<Entity.DeviceInfo>list=dev.GetPages(currentPage, pageSize, out counts, out pageNums);
                Entity.NVRDeviceInfo nvrEntity=new Entity.NVRDeviceInfo();
                foreach (Entity.DeviceInfo devItem in list)
                {
                    Entity.DeviceTable dt = new Entity.DeviceTable();
                    nvrEntity = nvr.GetNVRInfoBySn(devItem.Sn);
                    if (nvrEntity == null)
                    {
                        dt.username = "";
                        dt.deviceLoc = "";
                    }
                    else
                    {
                        dt.username = nvrEntity.username;
                        dt.deviceLoc = nvrEntity.deviceLoc;
                    }
                    dt.Sn = devItem.Sn;
                    dt.RemoteIPv4 = devItem.RemoteIPv4;
                    dt.IpLocal = GetIpLocation(dt.RemoteIPv4);
                    dt.LocalIpv4 = devItem.LocalIpv4;
                    dt.Mac = devItem.Mac;
                    dt.DeviceStatus = devItem.DeviceStatus == 1 ? "在线" : "离线";
                    dt.RegistTime = devItem.RegistTime;
                    dtList.Add(dt);
                }
            }
           /* else if (userType == "管理员")
            {

            }*/
            else if (userType=="普通用户")
            {
                List<Entity.NVRDeviceInfo> list = nvr.GetPages(currentPage, pageSize, out counts, out pageNums,username);
                foreach (Entity.NVRDeviceInfo nvrItem in list)
                {
                    Entity.DeviceTable dt = new Entity.DeviceTable();
                    Entity.DeviceInfo devEntity=dev.GetDeviceInfo(nvrItem.deviceSN);
                    if (devEntity!=null)
                    {
                        dt.username = nvrItem.username;
                        dt.deviceLoc = nvrItem.deviceLoc;
                        dt.Sn = devEntity.Sn;
                        dt.RemoteIPv4 = devEntity.RemoteIPv4;
                        dt.IpLocal = GetIpLocation(devEntity.RemoteIPv4);
                        dt.LocalIpv4 = devEntity.LocalIpv4;
                        dt.Mac = devEntity.Mac;
                        dt.DeviceStatus = devEntity.DeviceStatus == 1 ? "在线" : "离线";
                        dt.RegistTime = devEntity.RegistTime;
                        dtList.Add(dt);
                    }
                }
            }
#region 基于表联合后的比较实现，算法复杂，而且效率也不高
                      
            /*  List<Entity.DeviceInfo> list = dataFactory.DALDeviceInfoProvider.PartPageFunc(currentPage,pageSize,out counts,out pageNums);
           // List<Entity.NVRDeviceInfo> nvrList = dataFactory.DALNVRDevInfoProvider.LoadDVRInfoAndDevInfo(username);

            for (int i = 0; i < list.Count; i++)
            {
                Entity.DeviceTable dt = new Entity.DeviceTable();
                for (int j = 0; j < nvrList.Count; j++)
                {
                    if (list[i].Sn == nvrList[j].deviceSN)
                    {
                        dt.username = nvrList[j].username;
                        dt.deviceLoc = nvrList[j].deviceLoc;
                        break;
                    }
                    else
                    {
                        dt.username = "";
                        dt.deviceLoc = "";
                    }
                }
                dt.Sn = list[i].Sn;
                dt.RemoteIPv4 = list[i].RemoteIPv4;
                dt.IpLocal = GetIpLocation(dt.RemoteIPv4);
                dt.LocalIpv4=list[i].LocalIpv4;
                dt.Mac=list[i].Mac;
                dt.DeviceStatus=list[i].DeviceStatus==1?"在线":"离线";
                dt.RegistTime=list[i].RegistTime;
                dtList.Add(dt);
            }*/
#endregion

            return dtList;
        }

        public List<Entity.DeviceTable> GetDeviceTable(string sn)
        {
            NVRDevInfo nvr = new NVRDevInfo();
            DeviceInfo dev = new DeviceInfo();
            List<Entity.DeviceTable> list = new List<Entity.DeviceTable>();
            Entity.DeviceTable dt = new Entity.DeviceTable();
            Entity.DeviceInfo devEntity=dev.GetDeviceInfo(sn);
            Entity.NVRDeviceInfo nvrEntity = nvr.GetNVRInfoBySn(sn);
            if (nvrEntity == null)
            {
                dt.username = "";
                dt.deviceLoc = "";
            }
            else
            {
                dt.username = nvrEntity.username;
                dt.deviceLoc = nvrEntity.deviceLoc;
            }
            dt.Sn = devEntity.Sn;
            dt.RemoteIPv4 = devEntity.RemoteIPv4;
            dt.IpLocal = GetIpLocation(devEntity.RemoteIPv4);
            dt.LocalIpv4 = devEntity.LocalIpv4;
            dt.Mac = devEntity.Mac;
            dt.DeviceStatus = devEntity.DeviceStatus == 1 ? "在线" : "离线";
            dt.RegistTime = devEntity.RegistTime;
            list.Add(dt);
            return list;
            //dt.IpLocal = 
            //return dt;
           // List<Entity.DeviceTable> dtList = new List<Entity.DeviceTable>();
           // UserInfo user = new UserInfo();
          //  NVRDevInfo nvr = new NVRDevInfo();
           //eviceInfo dev = new DeviceInfo();

           // nvrEntity = nvr.GetNVRInfoBySn(devItem.Sn);
           /* if (nvrEntity == null)
            {
                dt.username = "";
                dt.deviceLoc = "";
            }
            else
            {
                dt.username = nvrEntity.username;
                dt.deviceLoc = nvrEntity.deviceLoc;
            }
            dt.Sn = devItem.Sn;
            dt.RemoteIPv4 = devItem.RemoteIPv4;
            dt.IpLocal = GetIpLocation(dt.RemoteIPv4);
            dt.LocalIpv4 = devItem.LocalIpv4;
            dt.Mac = devItem.Mac;
            dt.DeviceStatus = devItem.DeviceStatus == 1 ? "在线" : "离线";
            dt.RegistTime = devItem.RegistTime;
            return null;*/
        }
        /// <summary>
        /// 查询是sn是否在连接表查询中。如果存在返回true，否则返回false;
        /// </summary>
        /// <param name="sn"></param>
        /// <param name="nvrInfoList"></param>
        /// <returns></returns>
        private bool FindNVRListBySn(string sn, List<Entity.NVRDeviceInfo> nvrInfoList)
        {
            for (int i = 0; i < nvrInfoList.Count; i++)
            {
                if (sn == nvrInfoList[i].deviceSN)
                {
                    return true;
                }
            }
            return false;
        }

        private string GetIpLocation(string ip)
        {
            if (ip == "")
            {
                ip = System.Web.HttpContext.Current.Request.UserHostAddress;
            }
            string url = "http://www.youdao.com/smartresult-xml/search.s?type=ip&q=" + ip;
            string stringIpAddress = "";
            ////获取youdao返回的xml格式文件内容
            using (System.Xml.XmlReader read = System.Xml.XmlReader.Create(url))
            {
                while (read.Read())
                {
                    switch (read.NodeType)
                    {
                        case System.Xml.XmlNodeType.Text://取xml格式文件当中的文本内容
                            if (string.Format("{0}", read.Value).ToString().Trim() != ip)
                            {//youdao返回的xml格式文件内容一个是IP，另一个是IP地址，如果不是IP那么就是IP地址
                                stringIpAddress = string.Format("{0}", read.Value).ToString().Trim();
                            }
                            break;
                    }
                }
            }
            //HttpContext.Current.Response.Write(stringIpAddress);
            return stringIpAddress;
        }
    }
}
